<?php
include "header.php";
include "mysqli_connection.php";

if (!isset($_SESSION["username"])){
	echo "<script>window.location.href='login.php'</script>";
}
else{
	if (isset($_GET["id"]) && $_GET["id"] !== ""){
		$id = $mysqli->real_escape_string(trim($_GET["id"]));
		if ($id != 0){
			$query = "SELECT major, entry_year, starting_course FROM Programs WHERE p_id = ?;";
			$stmt = $mysqli->stmt_init();
			if ($stmt = $mysqli->prepare($query)){
				$stmt->bind_param("i", $id);
				$stmt->execute();
				$stmt->bind_result($major, $entry_year, $starting_course);
				if (!($stmt->fetch())){
					echo "<script>window.location.href='manageprograms.php'</script>";
				}
				$stmt->close();
			}
			$query = "SELECT Requirements.r_id, Requirements.name FROM Requirement_Program INNER JOIN Requirements WHERE (p_id = ?) AND (Requirement_Program.r_id = Requirements.r_id) ORDER BY Requirements.r_id;";
			$stmt = $mysqli->stmt_init();
			if ($stmt = $mysqli->prepare($query)){
				$stmt->bind_param("i", $id);
				$stmt->execute();
				$stmt->bind_result($r_id, $r_name);
			}
			$button_name = "Update";
		}
		else{
			$button_name = "Add";
		}
	}
	else{
		$id = 0;
		$button_name = "Add";
	}
?>
<script>change_picture("admin");</script>
<script>
var list = new Array();
var currentPage = 0;
function remove_requirement(id){
	var index = list.indexOf(id);
	if (index > -1) {
		list.splice(index, 1);
		$("#requirement_"+id).remove();
	}
	search_requirement(currentPage);
}
function add_requirement(id, name){
	var index = list.indexOf(id);
	if (index < 0) {
		list.push(id);
		$("#requirement_list").append("<tr id='requirement_"+id+"'><td>"+name+"</td><td><a href = '#' onclick = 'remove_requirement("+id+");false;'>Remove</a></td></tr>");
	}
	search_requirement(currentPage);
}
function search_requirement(n){
	$.post("findrequirements.php",
	{
		name:$("#search_name").val(),
		requirement_list:list,
		page:n,
	},
	function(data,status){
		$("#search_result").html(data);
	});
}
function submit_form(){
	$('#input_list').val(list);
	$('#updateprograms_form').submit();
}
function toggle_group_list(id){
	if ($("#toggle_group_link_"+id).text() == "Show"){
		$("#toggle_group_link_"+id).text("Hide");
		$.post("findgroups_byrequirement.php",
		{
			r_id:id,
		},
		function(data,status){
			$("#toggle_group_text_"+id).html(data);
		});
		$("#toggle_group_text_"+id).show();
	}
	else{
		$("#toggle_group_link_"+id).text("Show");
		$("#toggle_group_text_"+id).hide();
	}
}
</script>
<div>
<form id = "updateprograms_form" action = "updateprograms.php" method = "POST"/>
<input type = "hidden" name = "p_id" value = "<?php echo $id; ?>" />
<label for = "major">Major: </label><input type = "text" name = "major" value = "<?php echo $major; ?>" /><br />
<label for = "entry_year">Entry Year: </label><input type = "text" name = "entry_year" value = "<?php echo $entry_year; ?>" /><br />
<label for = "starting_course">Starting Course: </label>
<select name = "starting_course" >
<option value="" <?php if ($starting_course === "") echo "Selected";?>>(None)</option>
<option value="1" <?php if ($starting_course == 1) echo "Selected";?>>CS 105</option>
<option value="2" <?php if ($starting_course == 2) echo "Selected";?>>CS 115</option>
<option value="5" <?php if ($starting_course == 5) echo "Selected";?>>CS 181</option>
</select><br />
<input type = "hidden" name = "requirement_list" id = "input_list" />
<div>Requirement List:</div>
<div><a href = "#" onclick = "$('#search_list').show();false;">Add a requirement</a></div>
<div>
<table id="requirement_list">
<?php
	if (isset($stmt)){
		while ($stmt->fetch()){
			echo "<tr id='requirement_" . $r_id . "'><td>" . $r_name . "</td><td><a href = '#' onclick = 'remove_requirement(" . $r_id . ");false;'>Remove</a></td></tr>";
			echo "<script>list.push(" . $r_id . ");</script>";
		}
		$stmt->close();
	}
?>
</table>
</div>
<input type = "button" value = "Back" onclick = "window.location.href='manageprograms.php'" />
<input type = "button" value = "<?php echo $button_name;?>" onclick = "submit_form();" />
</form>
</div>
<div id = "search_list" style = "display:none;">
<div>
<input type = "button" value = "Close" onclick = "$('#search_list').hide();false;" />
</div>
<div>
<label for = "r_name">Name: </label><input type = "text" name = "r_name" id = "search_name" />
<input type = "button" value = "Search" onclick = "search_requirement(0);false;"/>
</div>
<div id = "search_result"></div>
</div>
<?php
}
include "footer.php";